package com.twd.nio;

import org.apache.catalina.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class MyServer {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel s=ServerSocketChannel.open();
        Selector server = Selector.open();
        s.bind(new InetSocketAddress(9999));
        s.configureBlocking(false);
        s.register(server,SelectionKey.OP_ACCEPT);
        Selector child = Selector.open();
        new  Thread(()->{
            try {
               while(true) {
                    System.out.println("wait.........");
                   try {
                       TimeUnit.SECONDS.sleep(5);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                    while (child.select() > 0) {
                        System.out.println("start.........");

                        Set<SelectionKey> selectionKeys1 = child.selectedKeys();
                        Iterator<SelectionKey> iterator1 = selectionKeys1.iterator();
                        while (iterator1.hasNext()) {
                            SelectionKey key1 = iterator1.next();
                            if (key1.isAcceptable()) {
                                System.out.println("*********");

                            } else if (key1.isReadable()) {
                                SocketChannel channel1 = (SocketChannel) key1.channel();

                                ByteBuffer allocate = ByteBuffer.allocate(1024);
                                channel1.read(allocate);
                                System.out.println("-----------");
//                                channel1.register(child,SelectionKey.OP_READ);
                            }
                            iterator1.remove();
                        }
//
                    }
                   Arrays.sort(new Integer[0], (a,b)-> {
                       return a - b;
                   });
                   System.out.println("ww");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }).start();
        while(server.select()>0){
            System.out.println("awwwwwww");
            Set<SelectionKey> selectionKeys = server.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while(iterator.hasNext()){
                SelectionKey key = iterator.next();
                if(key.isAcceptable()){
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                    SocketChannel accept = channel.accept();
                    accept.configureBlocking(false);
                    key.interestOps(SelectionKey.OP_READ);

//                    accept.register(server,SelectionKey.OP_READ);

                    System.out.println("aadasdasdasdasd");

                }else if(key.isReadable()){
                    //通过key 反向获取到对应channel
                    SocketChannel channel = (SocketChannel) key.channel();
                    //获取到该channel关联的buffer
                    ByteBuffer allocate = ByteBuffer.allocate(1024);
                    channel.read(allocate);
                    System.out.println("from 客户端"+new String(allocate.array()));
                    System.out.println("========");
                }
                iterator.remove();
            }
        }
    }
}
